package lesson19.notepad;

public class SimpleNotepad implements INotepad {

	Page[] simpleNotepad;

	public SimpleNotepad(final int numberOfPages) {
		if (numberOfPages > 0)
			this.simpleNotepad = new Page[numberOfPages];
		else
			this.simpleNotepad = new Page[20];

		for (int i = 0; i < simpleNotepad.length; i++) {
			simpleNotepad[i] = new Page(String.valueOf(i + 1));
		}
	}

	@Override
	public void addText(final String addedText, final int page) {
		if (validatePage(page)) {
			if (addedText != null) {
				simpleNotepad[page - 1].addText(addedText);
			} else
				System.out
						.println("Wrong text format! Please restart the app!");
		}
	}

	private boolean validatePage(int page) {
		return page > 0 && page <= simpleNotepad.length;
	}

	@Override
	public void overwriteText(final String newText, final int page) {
		if (validatePage(page))
			if (newText != null) {
				simpleNotepad[page - 1].deleteText();
				simpleNotepad[page - 1].addText(newText);
			} else
				System.out
						.println("Wrong text format! Please restart the app!");
	}

	@Override
	public void deleteText(final Page selectedPage) {
		if (selectedPage != null) {
			for (int i = 0; i < simpleNotepad.length; i++)
				if (simpleNotepad[i].equals(selectedPage))
					selectedPage.deleteText();
		}
	}

	@Override
	public void printPages() {
		for (int i = 0; i < simpleNotepad.length; i++) {
			//if (simpleNotepad[i] != null)
				simpleNotepad[i].printPageInfo();
		}

	}
	
	Page getPage(int pageNumber)
	{
		return this.simpleNotepad[pageNumber - 1]; 
	}

}
